<?php

/**
 * universidad actions.
 *
 * @package    auip
 * @subpackage universidad
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class reportesActions extends sfActions
{
  public function executeReporteUniversidad(sfWebRequest $request)
  {
    $this->universidads = Doctrine_Core::getTable('Universidad')->createQuery('a')->orderBy('id')->execute();
    $this->setLayout(false);
    $this->getResponse()->setContentType('application/msexcel');
    $this->setTemplate('reporteUniversidad');
    
  }
  
  public function executeReporteUniversidadPdf(sfWebRequest $request)
  {
      $items = Doctrine_Query::create()->from('Universidad u')->select('*, p.nombre as nombre1, r.nombre as nombre2')->innerJoin('u.Pais p')->innerJoin('u.Region r')->orderBy('id')->fetchArray();
      
      $listaItems = array('Id', 'Nombre', 'Pais', 'Region', 'Mora');
       $listaAncho = array(10, 80, 40, 38, 12);      
       $alto = 12;
       $mascara = array(0=>'id', 1=>'nombre', 2=>'nombre1', 3=>'nombre2', 4=>'en_mora');
       
       $this->reportes($listaItems, $listaAncho, $alto, $items, $mascara);
  }
  
  public function executeReporteUniversidadPdf2(sfWebRequest $request)
  { 
      //$items = Doctrine_Query::create()->from('Universidad u')->select('*, p.nombre as nombre1, r.nombre as nombre2')->innerJoin('u.Pais p')->innerJoin('u.Region r')->orderBy('id')->fetchArray();
      //$items = Doctrine_Query::create()->from('Universidad u')->addSelect(' count(*)')->addGroupBy('u.pais_id')->fetchArray();
      //$items = Doctrine_Query::create()->from('Universidad u')->Select('count(u.pais_id)')->orderBy('u.pais_id')->groupBy('u.pais_id')->fetchArray();
      //$items = Doctrine_Query::create()->from('Becas u')->Select(' u.pais_id, count(u.pais_id)')->groupBy('u.id, u.pais_id')->fetchArray();
      
      /*
      $items[0][0] = 'N0';
      $items[0][1] = 30;
      $items[1][0] = 'N1';
      $items[1][1] = 100;
      $items[2][0] = 'N2';
      $items[2][1] = 50;
      $items[3][0] = 'N3';
      $items[3][1] = 60;
      $items[4][0] = 'N4';
      $items[4][1] = 80;
      $items[5][0] = 'N5';
      $items[5][1] = 90;
      $items[6][0] = 'N6';
      $items[6][1] = 120;
      $items[7][0] = 'N7';
      $items[7][1] = 150;
      $items[8][0] = 'N8';
      $items[8][1] = 300;
      */
      
      $items = Doctrine_Query::create()->from('Universidad u')->Select('MIN(pais_id) as pais, count(u.pais_id) as cant')->groupBy('u.pais_id')->fetchArray();

      $datos2 =  Doctrine_Query::create()->from('Pais p')->Select('p.id as 1, p.nombre as 2')->orderBy('p.id')->fetchArray();
    
   for($i=0; $i<count($datos2); $i=$i+1)
   {
        $datos3[$datos2[$i][1]] = $datos2[$i][2];  
   }
   
   for($i=0; $i<count($items); $i=$i+1)
   {
        $items[$i]['pais'] = $datos3[$items[$i]['pais']];
   }
      
      $listaItems = array('Pais', 'Cantidad');
       $listaAncho = array(80,38);      
       $alto = 12;
       $mascara = array(0=>'pais', 1=>'cant');
       
       $this->reportesPie($listaItems, $listaAncho, $alto, $items, $mascara);
  }
  
  public function executeReporteUniversidadPdf3(sfWebRequest $request)
  { 
      
      $items = Doctrine_Query::create()->from('Universidad u')->Select('MIN(pais_id) as pais, count(u.pais_id) as cant')->groupBy('u.pais_id')->fetchArray();
      $datos2 =  Doctrine_Query::create()->from('Pais p')->Select('p.id as 1, p.nombre as 2')->orderBy('p.id')->fetchArray();
    
   for($i=0; $i<count($datos2); $i=$i+1)
   {
        $datos3[$datos2[$i][1]] = $datos2[$i][2];  
   }
   
   for($i=0; $i<count($items); $i=$i+1)
   {
        $items[$i]['pais'] = $datos3[$items[$i]['pais']];
   }
      
      $listaItems = array('Pais', 'Cantidad');
       $listaAncho = array(80,38);      
       $alto = 12;
       $mascara = array(0=>'pais', 1=>'cant');
       
       $this->reportesPieTwo($listaItems, $listaAncho, $alto, $items, $mascara);
  }
  
  protected function reportes($listaItems, $listaAncho, $alto, $items, $mascara)
  {

       $config = sfTCPDFPluginConfigHandler::loadConfig();
        sfTCPDFPluginConfigHandler::includeLangFile($this->getUser()->getCulture());

        $doc_title    = "test title";
        $doc_subject  = "test description";
        $doc_keywords = "test keywords";
        
        $htmlcontent = "";
        
        //create new PDF document (document units are set by default to millimeters)
        $pdf = new sfTCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor(PDF_AUTHOR);
        $pdf->SetTitle($doc_title);
        $pdf->SetSubject($doc_subject);
        $pdf->SetKeywords($doc_keywords);

        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

        //set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

        //set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); //set image scale factor

        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        //initialize document
        //$pdf->AliasNbPages();
        $pdf->AddPage();

        // set barcode
        $pdf->SetBarcode(date("Y-m-d H:i:s", time()));

        // output some HTML code
        $pdf->writeHTML($htmlcontent , true, 0);

        // output two html columns
        $first_column_width = 80;
        $current_y_position = $pdf->getY();
        
        for($i=0; $i<count($listaItems)-1; $i=$i+1)
        {
            $pdf->MultiCell($listaAncho[$i], $alto, $listaItems[$i], 1, 'J', 0, 0, '','', true, 0, true, true, 0, 'T', false);
        }
            $pdf->MultiCell($listaAncho[count($listaAncho)-1], $alto, $listaItems[count($listaAncho)-1], 1, 'J', 0);
            
        for($i=0; $i<count($items); $i=$i+1)
        {
            if($i%18 == 0)
            {
                if($i > 0)
                {
                    $pdf->AddPage();
                    for($k=0; $k<count($listaItems)-1; $k=$k+1)
                    {
                        $pdf->MultiCell($listaAncho[$k], $alto, $listaItems[$k], 1, 'J', 0, 0, '','', true, 0, true, true, 0, 'T', false);
                    }
                        $pdf->MultiCell($listaAncho[count($listaAncho)-1], $alto, $listaItems[count($listaAncho)-1], 1, 'J', 0);
                        
                }
            }
            
            for($j=0; $j<count($listaItems)-1; $j=$j+1)
            {
                $pdf->MultiCell($listaAncho[$j], $alto, "<P ALIGN='LEFT'>".$items[$i][$mascara[$j]]."</p>", 1, 'J', 0, 0, '','', true, 0, true, true, 0, 'T', false);
            }
                $pdf->MultiCell($listaAncho[count($listaAncho)-1], $alto, $items[$i][$mascara[count($listaAncho)-1]], 1, 'J', 0);
                
        }
        
        // output some UTF-8 test content
        // add page header/footer
        $pdf->setPrintHeader(true);
        $pdf->setPrintFooter(true);
        // Close and output PDF document
        $pdf->Output();
        // Stop symfony process
        throw new sfStopException();
  }
  
  
  protected function reportesPie($listaItems, $listaAncho, $alto, $items, $mascara)
  {

       $config = sfTCPDFPluginConfigHandler::loadConfig();
        sfTCPDFPluginConfigHandler::includeLangFile($this->getUser()->getCulture());

        $doc_title    = "test title";
        $doc_subject  = "test description";
        $doc_keywords = "test keywords";
        
        $htmlcontent = "";
        
        //create new PDF document (document units are set by default to millimeters)
        $pdf = new sfTCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);//(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor(PDF_AUTHOR);
        $pdf->SetTitle($doc_title);
        $pdf->SetSubject($doc_subject);
        $pdf->SetKeywords($doc_keywords);

        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

        //set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

        //set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); //set image scale factor

        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        //initialize document
        //$pdf->AliasNbPages();
        $pdf->AddPage();

        //CODIGO PARA HACER EL PIE
        
        $pdf->Write(0, 'LE PASTEL');
        
        $dato =count($items);
        $sum = 0;
        
        for($i=0; $i<$dato; $i=$i+1)
        {
            $sum = $sum + $items[$i][$mascara[1]];
        }
        
        $xc = 105;
        $yc = 100;
        $r = 60;

        $temp = 0;
        
        $rand1[0] = 0;
        $rand2[0] = 0;
        $rand3[0] = 0;
        
       
        
        for($i=0; $i<$dato; $i=$i+1)
        {
            $rand1[$i] = rand(0,255);
            $rand2[$i] = rand(0,255);
            $rand3[$i] = rand(0,255);
            
            $pdf->SetFillColor($rand1[$i], $rand2[$i], $rand3[$i]);
            $pdf->SetTextColor($rand1[$i], $rand2[$i], $rand3[$i]);
            $pdf->PieSector($xc, $yc, $r, $temp, $temp + (360/$sum)*$items[$i][$mascara[1]], 'FD', false, 0, 2);
            
            $pdf->Text(30, 165+(5*$i), $items[$i][$mascara[0]]." => ".$items[$i][$mascara[1]]." => ".round(($items[$i][$mascara[1]]*100/$sum),2)." %");
            
            //extraña funcion para poner los porcentajes alrededor del circulo
            $pdf->SetFontSize(10);
            $pdf->Text($xc -3 + cos(3.14159265359*($temp + (360/($sum*1.7))*$items[$i][$mascara[1]])/180)*(($r+10)), $yc - sin((3.14159265359*($temp + (360/($sum*1.7))*$items[$i][$mascara[1]]))/180)*(($r+5 + ($i%2)*3)), round(($items[$i][$mascara[1]]*100/$sum),1)."%");
            $pdf->SetFontSize(12);
            $temp = $temp + (360/$sum)*$items[$i][$mascara[1]];
            
        }
            $pdf->SetTextColor(0,0,0);
            $pdf->Text(30, 165+(5*$dato), "Total => ".$sum." => 100 %");

        $pdf->AddPage();
        $pdf->Write(0, 'LE BARRAS');
        
        $estiloCuadrado = array('L' => 10, 'T' => 10, 'R' => 10, 'B' => 10);
        //$pdf->Rect(100, 10, 40, 20, 'DF', $estiloCuadrado, array(220, 220, 200));
        
        //busqueda del maximo
        $max = 0;
        for($i=0; $i<$dato; $i=$i+1)
        {
            if($max<$items[$i][$mascara[1]])
            {
                $max=$items[$i][$mascara[1]];
            }
        }
        //Barras y algunos textos
        for($i=0; $i<$dato; $i=$i+1)
        {
            $pdf->Rect(30+(($i)*(150/$dato)), 40+(120-(($items[$i][$mascara[1]]/$max)*120)) , ((150/$dato))-1, (($items[$i][$mascara[1]]/$max)*120), 'DF', $estiloCuadrado, array($rand1[$i], $rand2[$i], $rand3[$i]));
            $pdf->SetTextColor($rand1[$i], $rand2[$i], $rand3[$i]);
            $pdf->Text(30, 160+(5*$i), $items[$i][$mascara[0]]." => ".$items[$i][$mascara[1]]." => ".round(($items[$i][$mascara[1]]*100/$sum),2)." %");
            $pdf->SetFontSize(10);
            $pdf->Text(28+(($i)*(150/$dato)), 35+(120-(($items[$i][$mascara[1]]/$max)*120)) , round(($items[$i][$mascara[1]]*100/$sum),1)."%");
            $pdf->SetFontSize(12);
        }
        //LINEA
            $pdf->Rect(29, 40, 0, 120, 'DF', $estiloCuadrado, array(0,0,0));
        
        //Texto al lado de la linea
        for($i=0; $i<$dato; $i=$i+1)
        {
            $pdf->SetTextColor(0,0,0);
            $pdf->Text(20, 155-($i/$dato)*118, round(($i/$dato)*$sum)+" X");
        }
            $pdf->Text(20, 37, $sum+" X");

            //TOTAL
            $pdf->SetTextColor(0,0,0);
            $pdf->Text(30, 160+(5*$dato), "Total => ".$sum." => 100 %");    
            
        // add page header/footer
        $pdf->setPrintHeader(true);
        $pdf->setPrintFooter(true);
        // Close and output PDF document
        $pdf->Output();
        // Stop symfony process
        throw new sfStopException();
  }
  
  protected function reportesPieTwo($listaItems, $listaAncho, $alto, $items, $mascara)
  {

       $config = sfTCPDFPluginConfigHandler::loadConfig();
        sfTCPDFPluginConfigHandler::includeLangFile($this->getUser()->getCulture());

        $doc_title    = "Reportes AUIP";
        $doc_subject  = "Reportes";
        $doc_keywords = "Reports, reportes, reporter extremo";
        
        $htmlcontent = "";
        
        //create new PDF document (document units are set by default to millimeters)
        $pdf = new sfTCPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true);//(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor(PDF_AUTHOR);
        $pdf->SetTitle($doc_title);
        $pdf->SetSubject($doc_subject);
        $pdf->SetKeywords($doc_keywords);

        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

        //set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

        //set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); //set image scale factor

        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        //initialize document
        //$pdf->AliasNbPages();
        $pdf->AddPage();

        //CODIGO PARA HACER EL PIE
        
        $pdf->Write(0, 'LE PASTEL');
        
        $dato =count($items);
        $sum = 0;
        
        for($i=0; $i<$dato; $i=$i+1)
        {
            $sum = $sum + $items[$i][$mascara[1]];
        }
        
        $xc = 90;
        $yc = 100;
        $r = 60;

        $temp = 0;
        
        $rand1[0] = 0;
        $rand2[0] = 0;
        $rand3[0] = 0;
        
       
        
        for($i=0; $i<$dato; $i=$i+1)
        {
            $rand1[$i] = rand(0,255);
            $rand2[$i] = rand(0,255);
            $rand3[$i] = rand(0,255);
            
            $pdf->SetFillColor($rand1[$i], $rand2[$i], $rand3[$i]);
            $pdf->SetTextColor($rand1[$i], $rand2[$i], $rand3[$i]);
            $pdf->PieSector($xc, $yc, $r, $temp, $temp + (360/$sum)*$items[$i][$mascara[1]], 'FD', false, 0, 2);
            
            $pdf->Text(170, 30+(5*$i), $items[$i][$mascara[0]]." => ".$items[$i][$mascara[1]]." => ".round(($items[$i][$mascara[1]]*100/$sum),2)." %");
            
            //extraña funcion para poner los porcentajes alrededor del circulo
            $pdf->SetFontSize(10);
            $pdf->Text($xc -3 + cos(3.14159265359*($temp + (360/($sum*1.7))*$items[$i][$mascara[1]])/180)*(($r+10)), $yc - sin((3.14159265359*($temp + (360/($sum*1.7))*$items[$i][$mascara[1]]))/180)*(($r+5 + ($i%2)*3)), round(($items[$i][$mascara[1]]*100/$sum),1)."%");
            $pdf->SetFontSize(12);
            $temp = $temp + (360/$sum)*$items[$i][$mascara[1]];
            
        }
            $pdf->SetTextColor(0,0,0);
            $pdf->Text(170, 30+(5*$dato), "Total => ".$sum." => 100 %");

        $pdf->AddPage();
        $pdf->Write(0, 'LE BARRAS');
        
        $estiloCuadrado = array('L' => 10, 'T' => 10, 'R' => 10, 'B' => 10);
        //$pdf->Rect(100, 10, 40, 20, 'DF', $estiloCuadrado, array(220, 220, 200));
        
        //busqueda del maximo
        $max = 0;
        for($i=0; $i<$dato; $i=$i+1)
        {
            if($max<$items[$i][$mascara[1]])
            {
                $max=$items[$i][$mascara[1]];
            }
        }
        //Barras y algunos textos
        for($i=0; $i<$dato; $i=$i+1)
        {
            $pdf->Rect(30+(($i)*(150/$dato)), 40+(120-(($items[$i][$mascara[1]]/$max)*120)) , ((150/$dato))-1, (($items[$i][$mascara[1]]/$max)*120), 'DF', $estiloCuadrado, array($rand1[$i], $rand2[$i], $rand3[$i]));
            $pdf->SetTextColor($rand1[$i], $rand2[$i], $rand3[$i]);
            $pdf->Text(190, 30+(5*$i), $items[$i][$mascara[0]]." => ".$items[$i][$mascara[1]]." => ".round(($items[$i][$mascara[1]]*100/$sum),2)." %");
            $pdf->SetFontSize(10);
            $pdf->Text(28+(($i)*(150/$dato)), 35+(120-(($items[$i][$mascara[1]]/$max)*120)) , round(($items[$i][$mascara[1]]*100/$sum),1)."%");
            $pdf->SetFontSize(12);
        }
        //LINEA
            $pdf->Rect(29, 40, 0, 120, 'DF', $estiloCuadrado, array(0,0,0));
        
        //Texto al lado de la linea
        for($i=0; $i<$dato; $i=$i+1)
        {
            $pdf->SetTextColor(0,0,0);
            $pdf->Text(20, 155-($i/$dato)*118, round(($i/$dato)*$sum)+" X");
        }
            $pdf->Text(20, 37, $sum+" X");

            //TOTAL
            $pdf->SetTextColor(0,0,0);
            $pdf->Text(190, 30+(5*$dato), "Total => ".$sum." => 100 %");    
            
        // add page header/footer
        $pdf->setPrintHeader(true);
        $pdf->setPrintFooter(true);
        // Close and output PDF document
        $pdf->Output();
        // Stop symfony process
        throw new sfStopException();
  }
  
  
  public function executeIndex(sfWebRequest $request)
  {
    $this->universidads = Doctrine_Core::getTable('Universidad')->createQuery('a')->limit(30)->execute();      
    
  }
  
  public function executeIndex2(sfWebRequest $request)
  {
    //$this->universidads = Doctrine_Core::getTable('Universidad')->createQuery('a')->limit(30)->execute();      
    $this->datos =  Doctrine_Query::create()->from('Universidad u')->Select('MIN(pais_id) as pais, count(u.pais_id) as cant')->groupBy('u.pais_id')->fetchArray();
    
    //-------------PEDAZO COPIABLE PARA EL CAMBIO DE DATOS DE OTRAS TABLAS----------------
    $datos2 =  Doctrine_Query::create()->from('Pais p')->Select('p.id as 1, p.nombre as 2')->orderBy('p.id')->fetchArray();
   for($i=0; $i<count($datos2); $i=$i+1)
   {
        $datos3[$datos2[$i][1]] = $datos2[$i][2];  
   }
   for($i=0; $i<count($this->datos); $i=$i+1)
   {
        $this->datos[$i]['pais'] = $datos3[$this->datos[$i]['pais']];
   }
    //-------------PEDAZO COPIABLE PARA EL CAMBIO DE DATOS DE OTRAS TABLAS----------------

        $this->listaItems = array('Pais', 'Cantidad');
       $this->mascara = array(0=>'pais', 1=>'cant');
    
    $this->setLayout(false);
    $this->getResponse()->setContentType('application/msexcel');
    $this->setTemplate('index2');
    
    
    
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->universidad = Doctrine_Core::getTable('Universidad')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->universidad);
  }


  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $universidad = $form->save();

      //$this->redirect('universidad/edit?id='.$universidad->getId());
      $this->redirect('becas/entidad');            
    }
  }
}
